package cn.edu.swu.zwy.security;

import cn.edu.swu.zwy.book.AuthCodeServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;

public class LoginServlet extends HttpServlet {

    public  void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user=request.getParameter("user");
        String pass=request.getParameter("pass");
        String code=request.getParameter("code");
        response.setContentType("text/html");
        HttpSession session=request.getSession(true);

        if(code==null|| !code.equals(session.getAttribute(AuthCodeServlet.AUTH_CODE))){
            response.sendRedirect("./index.html");
            return;
        }

        if(user !=null&&user.equals("admin")){
            if(pass!=null&&pass.equals("123456")){

                session.setAttribute(AuthFilter.AUTH_STATUS,AuthStatus.LOGIN_SUCCESS);

                //返回一个重定向
                response.sendRedirect("./main");
                return;
                //返回HTML代码
                /*
                try(Writer writer=response.getWriter()){
                    writer.write("<html><head></head><body><center><br><br>");
                    writer.write("<h1 style='color:red'>欢迎用户从"+request.getRemoteAddr() +"登录成功 !</h1><br><br>");
                    writer.write(this.printParameters(request));
                    writer.write("<br><br>");
                    writer.write(this.printHeaders(request));
                    writer.write("</center></body></html>");
                 */

                }
            }

        //response.sendRedirect("./index.html");
        //response.setIntHeader("Refresh",5);
        response.setContentType("text/html");
        response.setHeader("Refresh","5;url=./index.html");
        try(Writer writer=response.getWriter()){
            writer.write("<center><h1 style='color:red'>用户名/密码错误！！请重新登录！</h1></center>");
        }
        //response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"用户名/密码错误");
    }

    private String printParameters(HttpServletRequest request) throws IOException {
        StringBuilder sb=new StringBuilder();
        sb.append("HTTP Parameters Table <br><table border=0 style='width:70%' cellpadding='1em'>");
        Enumeration<String> names= request.getParameterNames();
        while(names.hasMoreElements()){
            String name=names.nextElement();
            String value=request.getParameter(name);
            sb.append(String.format("<tr style='background-color:#eee'><td width='200px'> %s</td><td> %s</td></tr>",name,value));
        }
        sb.append("</table>");
        return sb.toString();
    }
    private String printHeaders(HttpServletRequest request) throws IOException {
        StringBuilder sb=new StringBuilder();
        sb.append("HTTP Header Table <br><table border=0 style='width:70%' cellpadding='1em'>");
        Enumeration<String> names= request.getHeaderNames();
        while(names.hasMoreElements()){
            String name=names.nextElement();
            String value=request.getHeader(name);
            sb.append(String.format("<tr style='background-color:#eee'><td width='200px'> %s</td><td> %s</td></tr>",name,value));
        }
        sb.append("</table>");
        return sb.toString();
    }
}
